const apiStatus = require('../../apiStatus');
const validHelper = require('../../helpers/validHelper');
const passportService = require('./passportService');
const security = require('../../providers/security');

function setup(app, appRouter){
    appRouter.post('/cms/passport/login', async(ctx,next) => {
        var appId = ctx.appInfo.id;
        var userName = ctx.request.body.userName;
        var password = ctx.request.body.password;
        var miss = [];
        miss = validHelper.check([
          userName,"userName",
          password,"password"
        ]);
        if (miss.length > 0) {
          ctx.body = {apiStatus: apiStatus.argumentInvalid};
          return;
        }

        const profile = await passportService.login(userName,security.md5(password));
        if (profile) {
          ctx.apiBody = profile;
        } else {
          ctx.body = {apiStatus: apiStatus.loginInvalid};
        }
        next();
    });
}

module.exports = setup;
